﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
using University.Workshop.Business;
using University.Workshop.Model;

namespace University.Workshop.Presenters
{
    public class PresenterReportPurchase
    {
        private IReportPurchase Report;
        UserLogOn user = UserLogOn.Instance;
        public PresenterReportPurchase(IReportPurchase report)
        {
            Report = report;  
        }

        public bool EnableAndDisableControls()
        {
            if (Report.FilterBy == "Todos")
            {
                Report.EnabledProductGroup = false;
                Report.EnabledProviderGroup = false;
                Report.EnabledDateFrom = false;
                Report.EnabledDateTo = false;
            }
            else
            {
                Report.EnabledDateFrom = true;
                Report.EnabledDateTo = true;
                if (Report.FilterBy == "Productos")
                {
                    Report.EnabledProductGroup = true;
                    Report.EnabledProviderGroup = false;
                }
                else
                {
                    if (Report.FilterBy == "Proveedores")
                    {
                        Report.EnabledProductGroup = false;
                        Report.EnabledProviderGroup = true;
                    }
                    else
                    {
                        Report.EnabledProductGroup = false;
                        Report.EnabledProviderGroup = false;
                    }
                }
            }
            return true;
        }

        public string OnGenerateReport()
        {
            if (Report.FilterBy == "Todos")
            {
                if (GenerateReportAllPurchases())
                    return "Reporte generado exitosamente!";
                else
                    return "No existen compras!";
            }
            else
            {
                if (Report.FilterBy == "Productos")
                {
                    if (this.GenerateReportOfASpecificProductFromTo())
                        return "Reporte generado exitosamente!";
                    else
                        return "No existen compras, con el producto seleccionado o en el rango de fechas!";
                }
                else
                {
                    if (Report.FilterBy == "Proveedores")
                    {
                        if(GenerateReportOfASpecificProviderFromTo())
                            return "Reporte generado exitosamente!";
                        else
                            return "No existen compras, con el proveedor seleccionado o en el rango de fechas!";
                    }
                    else
                    {
                        if (Report.FilterBy == "Fechas")
                        {
                            if(GenerateReportOfAllPurchasesByDates())
                                return "Reporte generado exitosamente!";
                            else
                                return "No existen compras en el rango de fechas seleccionado!";
                        }
                        else
                            return "El filtro esta vacio, por favor seleccione uno!";
                        
                    }
                }
            }
        }

        private bool GenerateReportOfAllPurchasesByDates()
        {
            DataTable data = PurchaseReportControl.GetAllPurchaseProductsBetweenDates(Report.DateFrom, Report.DateTo);
            if (data.Rows.Count != 0)
            {
                DataTable dataDetail;

                Document document = new Document();
                FileStream file = new FileStream("reporteCompraFechas.pdf", FileMode.OpenOrCreate);
                try
                {
                    PdfWriter.GetInstance(document, file);
                    document.Open();
                    Paragraph paragraphLogo = new Paragraph();
                    Paragraph paragraphTitle = new Paragraph();
                    Paragraph paragraphUser = new Paragraph();
                    Paragraph paragraphSubtitle = new Paragraph();
                    Chunk logo = new Chunk("Pag " + (document.PageNumber + 1) + "                                                                                          FARMACIA                                               Fecha: " + System.DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 9));
                    Chunk title = new Chunk("Detalle de compra de mercaderia", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
                    Chunk subtitle = new Chunk("Desde " + Report.DateFrom.ToShortDateString() + " - Hasta " + Report.DateTo.ToShortDateString(), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 15));
                    Chunk userReport = new Chunk("Reporte generado por: " + user.Name + " " + user.Surname1 + " " + user.Surname2, FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8));
                    paragraphLogo.Add(logo);
                    paragraphTitle.Add(title);
                    paragraphTitle.Alignment = Element.ALIGN_CENTER;
                    paragraphSubtitle.Add(subtitle);
                    paragraphSubtitle.Alignment = Element.ALIGN_CENTER;
                    paragraphUser.Add(userReport);
                    paragraphUser.Alignment = Element.ALIGN_LEFT;
                    document.Add(paragraphLogo);
                    document.Add(new Paragraph(" "));
                    document.Add(paragraphTitle);
                    document.Add(paragraphSubtitle);
                    document.Add(paragraphUser);
                    document.Add(new Paragraph(" "));
                    document.Add(new Paragraph("---------------------------------------------------------------------------------------------------------------------------------"));

                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        Chunk purchase = new Chunk(new Chunk("Nº Factura: " + data.Rows[i].ItemArray[0].ToString() + "      Proveedor: " + data.Rows[i].ItemArray[1].ToString() + "      Fecha compra: " + data.Rows[i].ItemArray[2].ToString() + "      Usuario: " + data.Rows[i].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8)));
                        Chunk amount = new Chunk(new Chunk("Monto Total.-                                                                                                                                                                             " + data.Rows[i].ItemArray[4].ToString() + "Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8)));

                        document.Add(new Paragraph(purchase));
                        dataDetail = PurchaseReportControl.GetASpecificDetailPurchases(data.Rows[i].ItemArray[0].ToString());
                        PdfPTable table = new PdfPTable(5);
                        document.Add(new Paragraph(" "));
                        Chunk h1 = new Chunk("Cantidad", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h2 = new Chunk("Codigo", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h3 = new Chunk("Descripcion", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h4 = new Chunk("C/u Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h5 = new Chunk("Importe Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        table.AddCell(new Phrase(h1));
                        table.AddCell(new Phrase(h2));
                        table.AddCell(new Phrase(h3));
                        table.AddCell(new Phrase(h4));
                        table.AddCell(new Phrase(h5));
                        for (int j = 0; j < dataDetail.Rows.Count; j++)
                        {
                            Chunk c1 = new Chunk(dataDetail.Rows[j].ItemArray[0].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c2 = new Chunk(dataDetail.Rows[j].ItemArray[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c3 = new Chunk(dataDetail.Rows[j].ItemArray[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c4 = new Chunk(dataDetail.Rows[j].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c5 = new Chunk(dataDetail.Rows[j].ItemArray[4].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            table.AddCell(new Phrase(c1));
                            table.AddCell(new Phrase(c2));
                            table.AddCell(new Phrase(c3));
                            table.AddCell(new Phrase(c4));
                            table.AddCell(new Phrase(c5));
                        }
                        document.Add(table);
                        document.Add(new Paragraph(amount));
                    }
                    document.Close();
                    System.Diagnostics.Process.Start("reporteCompraFechas.pdf");
                    return true;
                }
                finally
                {
                    if (file != null)
                        file.Dispose();
                    if (document != null)
                        document.Dispose();
                }
            }
            else
                return false;
        }

        private bool GenerateReportOfASpecificProviderFromTo()
        {
            DataTable data = PurchaseReportControl.GetAnSpecificProviderBetweenFromTo(Report.OfProvider, Report.DateFrom, Report.DateTo);
            if (data.Rows.Count != 0)
            {
                double amount = 0;
                Document documentProduct = new Document();
                FileStream file = new FileStream("reporteCompraProveedorFechas.pdf", FileMode.OpenOrCreate);
                try
                {
                    PdfWriter.GetInstance(documentProduct, file);
                    documentProduct.Open();
                    Paragraph paragraphLogo = new Paragraph();
                    Paragraph paragraphTitle = new Paragraph();
                    Paragraph paragraphSubtitle = new Paragraph();
                    Paragraph paragraphProviderName = new Paragraph();
                    Chunk logo = new Chunk("Pag " + (documentProduct.PageNumber + 1) + "                                                                   FARMACIA                                               Fecha: " + System.DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 9));
                    Chunk title = new Chunk("Detalle de compra de mercaderia", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
                    Chunk subtitle = new Chunk("Desde " + Report.DateFrom.ToShortDateString() + " - Hasta " + Report.DateTo.ToShortDateString(), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 15));
                    Chunk providerName = new Chunk("Proveedor: " + PurchaseReportControl.GetProviderNameByCode(Report.OfProvider), FontFactory.GetFont(FontFactory.HELVETICA, 11));
                    paragraphLogo.Add(logo);
                    paragraphLogo.Alignment = Element.ALIGN_CENTER;
                    paragraphTitle.Add(title);
                    paragraphTitle.Alignment = Element.ALIGN_CENTER;
                    paragraphSubtitle.Add(subtitle);
                    paragraphSubtitle.Alignment = Element.ALIGN_CENTER;
                    paragraphProviderName.Add(providerName);
                    documentProduct.Add(paragraphLogo);
                    documentProduct.Add(new Paragraph(" "));
                    documentProduct.Add(paragraphTitle);
                    documentProduct.Add(paragraphSubtitle);
                    documentProduct.Add(paragraphProviderName);
                    documentProduct.Add(new Paragraph("---------------------------------------------------------------------------------------------------------------------------------"));
                    PdfPTable table = new PdfPTable(6);
                    documentProduct.Add(new Paragraph(" "));
                    Chunk h0 = new Chunk("Fecha", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h1 = new Chunk("Nº Factura", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h2 = new Chunk("Producto", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h3 = new Chunk("Cantidad", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h4 = new Chunk("C/u Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h5 = new Chunk("Importe Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    table.AddCell(new Phrase(h0));
                    table.AddCell(new Phrase(h1));
                    table.AddCell(new Phrase(h2));
                    table.AddCell(new Phrase(h3));
                    table.AddCell(new Phrase(h4));
                    table.AddCell(new Phrase(h5));
                    for (int j = 0; j < data.Rows.Count; j++)
                    {
                        Chunk c1 = new Chunk(data.Rows[j].ItemArray[0].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c2 = new Chunk(data.Rows[j].ItemArray[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c3 = new Chunk(data.Rows[j].ItemArray[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c4 = new Chunk(data.Rows[j].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c5 = new Chunk(data.Rows[j].ItemArray[4].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c6 = new Chunk(data.Rows[j].ItemArray[5].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        table.AddCell(new Phrase(c1));
                        table.AddCell(new Phrase(c2));
                        table.AddCell(new Phrase(c3));
                        table.AddCell(new Phrase(c4));
                        table.AddCell(new Phrase(c5));
                        table.AddCell(new Phrase(c6));
                        string varible = data.Rows[j].ItemArray[5].ToString();
                        amount = amount + Convert.ToDouble(varible, System.Globalization.CultureInfo.CurrentCulture);
                    }
                    documentProduct.Add(table);
                    Chunk mon = new Chunk("Monto Total.-                                                                                                                                                                         " + amount.ToString(System.Globalization.CultureInfo.CurrentCulture) + "Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8));
                    documentProduct.Add(mon);
                    documentProduct.Close();
                    System.Diagnostics.Process.Start("reporteCompraProveedorFechas.pdf");
                    return true;
                }
                finally
                {
                    if (file != null)
                        file.Dispose();
                    if (documentProduct != null)
                        documentProduct.Dispose();
                }
            }
            else
                return false;
        }

        private bool GenerateReportOfASpecificProductFromTo()
        {
            DataTable data = PurchaseReportControl.GetAnSpecificProductBetweenFromTo(Report.OfProduct, Report.DateFrom, Report.DateTo);
            if (data.Rows.Count != 0)
            {
                double amount = 0;
                Document documentProduct = new Document();
                FileStream file = new FileStream("reporteCompraProductoFechas.pdf", FileMode.OpenOrCreate);
                try
                {
                    PdfWriter.GetInstance(documentProduct, file);
                    documentProduct.Open();
                    Paragraph paragraphLogo = new Paragraph();
                    Paragraph paragraphTitle = new Paragraph();
                    Paragraph paragraphSubtitle = new Paragraph();
                    Paragraph paragraphProductName = new Paragraph();
                    Chunk logo = new Chunk("Pag " + (documentProduct.PageNumber + 1) + "                                                                             FARMACIA                                               Fecha: " + System.DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 9));
                    Chunk title = new Chunk("Detalle de compra de mercaderia", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
                    Chunk subtitle = new Chunk("Desde " + Report.DateFrom.ToShortDateString() + " - Hasta " + Report.DateTo.ToShortDateString(), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 15));
                    Chunk productName = new Chunk("Producto: " + PurchaseReportControl.GetProductNameByCode(Report.OfProduct), FontFactory.GetFont(FontFactory.HELVETICA, 11));
                    paragraphLogo.Add(logo);
                    paragraphLogo.Alignment = Element.ALIGN_CENTER;
                    paragraphTitle.Add(title);
                    paragraphTitle.Alignment = Element.ALIGN_CENTER;
                    paragraphSubtitle.Add(subtitle);
                    paragraphSubtitle.Alignment = Element.ALIGN_CENTER;
                    paragraphProductName.Add(productName);
                    documentProduct.Add(paragraphLogo);
                    documentProduct.Add(new Paragraph(" "));
                    documentProduct.Add(paragraphTitle);
                    documentProduct.Add(paragraphSubtitle);
                    documentProduct.Add(paragraphProductName);
                    documentProduct.Add(new Paragraph("---------------------------------------------------------------------------------------------------------------------------------"));
                    PdfPTable table = new PdfPTable(6);
                    documentProduct.Add(new Paragraph(" "));
                    Chunk h0 = new Chunk("Fecha", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h1 = new Chunk("Nº Factura", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h2 = new Chunk("Proveedor", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h3 = new Chunk("Cantidad", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h4 = new Chunk("C/u Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    Chunk h5 = new Chunk("Importe Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8));
                    table.AddCell(new Phrase(h0));
                    table.AddCell(new Phrase(h1));
                    table.AddCell(new Phrase(h2));
                    table.AddCell(new Phrase(h3));
                    table.AddCell(new Phrase(h4));
                    table.AddCell(new Phrase(h5));
                    for (int j = 0; j < data.Rows.Count; j++)
                    {
                        Chunk c1 = new Chunk(data.Rows[j].ItemArray[0].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c2 = new Chunk(data.Rows[j].ItemArray[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c3 = new Chunk(data.Rows[j].ItemArray[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c4 = new Chunk(data.Rows[j].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c5 = new Chunk(data.Rows[j].ItemArray[4].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        Chunk c6 = new Chunk(data.Rows[j].ItemArray[5].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                        table.AddCell(new Phrase(c1));
                        table.AddCell(new Phrase(c2));
                        table.AddCell(new Phrase(c3));
                        table.AddCell(new Phrase(c4));
                        table.AddCell(new Phrase(c5));
                        table.AddCell(new Phrase(c6));
                        string varible = data.Rows[j].ItemArray[5].ToString();
                        amount = amount + Convert.ToDouble(varible, System.Globalization.CultureInfo.CurrentCulture);
                    }
                    documentProduct.Add(table);
                    Chunk mon = new Chunk("Monto Total.-                                                                                                                                                                         " + amount.ToString(System.Globalization.CultureInfo.CurrentCulture) + "Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8));
                    documentProduct.Add(mon);
                    documentProduct.Close();
                    System.Diagnostics.Process.Start("reporteCompraProductoFechas.pdf");
                    return true;
                }
                finally
                {
                    if (file != null)
                        file.Dispose();
                    if (documentProduct != null)
                        documentProduct.Dispose();
                }
            }
            else
                return false;
        }

        private bool GenerateReportAllPurchases()
        {
            DataTable data = PurchaseReportControl.GetAllPurchaseProducts(string.Empty);
            if (data.Rows.Count != 0)
            {
                DataTable dataDetail;
                                Document document = new Document();
                FileStream file = new FileStream("reporteCompraTodos.pdf", FileMode.OpenOrCreate);
                try
                {
                    PdfWriter.GetInstance(document, file);
                    document.Open();
                    Paragraph paragraphLogo = new Paragraph();
                    Paragraph paragraphTitle = new Paragraph();
                    Paragraph paragraphUser = new Paragraph();
                    Paragraph paragraphSubtitle = new Paragraph();
                    Chunk logo = new Chunk("Pag " + (document.PageNumber + 1) + "                                                                                          FARMACIA                                               Fecha: " + System.DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 9));
                    Chunk title = new Chunk("Detalle de compra de mercaderia", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
                    Chunk subtitle = new Chunk("Todos", FontFactory.GetFont(FontFactory.HELVETICA, 16));
                    Chunk userReport = new Chunk("Reporte generado por: " + user.Name + " " + user.Surname1 + " " + user.Surname2, FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8));
                    paragraphLogo.Add(logo);
                    paragraphTitle.Add(title);
                    paragraphTitle.Alignment = Element.ALIGN_CENTER;
                    paragraphSubtitle.Add(subtitle);
                    paragraphSubtitle.Alignment = Element.ALIGN_CENTER;
                    paragraphUser.Add(userReport);
                    paragraphUser.Alignment = Element.ALIGN_LEFT;
                    document.Add(paragraphLogo);
                    document.Add(new Paragraph(" "));
                    document.Add(paragraphTitle);
                    document.Add(paragraphSubtitle);
                    document.Add(paragraphUser);
                    document.Add(new Paragraph(" "));
                    document.Add(new Paragraph("---------------------------------------------------------------------------------------------------------------------------------"));

                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        Chunk purchase = new Chunk(new Chunk("Nº Factura: " + data.Rows[i].ItemArray[0].ToString() + "      Proveedor: " + data.Rows[i].ItemArray[1].ToString() + "      Fecha compra: " + data.Rows[i].ItemArray[2].ToString() + "      Usuario: " + data.Rows[i].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8)));
                        Chunk amount = new Chunk(new Chunk("Monto Total.-                                                                                                                                                                             " + data.Rows[i].ItemArray[4].ToString() + "Bs.-", FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8)));

                        document.Add(new Paragraph(purchase));
                        dataDetail = PurchaseReportControl.GetASpecificDetailPurchases(data.Rows[i].ItemArray[0].ToString());
                        PdfPTable table = new PdfPTable(5);
                        document.Add(new Paragraph(" "));
                        Chunk h1 = new Chunk("Cantidad", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h2 = new Chunk("Codigo", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h3 = new Chunk("Descripcion", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h4 = new Chunk("C/u Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        Chunk h5 = new Chunk("Importe Bs.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11));
                        table.AddCell(new Phrase(h1));
                        table.AddCell(new Phrase(h2));
                        table.AddCell(new Phrase(h3));
                        table.AddCell(new Phrase(h4));
                        table.AddCell(new Phrase(h5));
                        for (int j = 0; j < dataDetail.Rows.Count; j++)
                        {
                            Chunk c1 = new Chunk(dataDetail.Rows[j].ItemArray[0].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c2 = new Chunk(dataDetail.Rows[j].ItemArray[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c3 = new Chunk(dataDetail.Rows[j].ItemArray[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c4 = new Chunk(dataDetail.Rows[j].ItemArray[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            Chunk c5 = new Chunk(dataDetail.Rows[j].ItemArray[4].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 8));
                            table.AddCell(new Phrase(c1));
                            table.AddCell(new Phrase(c2));
                            table.AddCell(new Phrase(c3));
                            table.AddCell(new Phrase(c4));
                            table.AddCell(new Phrase(c5));
                        }
                        document.Add(table);
                        document.Add(new Paragraph(amount));
                    }
                    document.Close();
                    System.Diagnostics.Process.Start("reporteCompraTodos.pdf");
                    return true;
                }
                finally
                {
                    if (file != null)
                        file.Dispose();
                    if (document != null)
                        document.Dispose();
                }
            }
            else
                return false;
        }
    }
}
